package com.bkfm.framework.core.rest;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yak.core.provider.TreeConfigCache;
import org.yak.core.store.PageParam;
import org.yak.core.web.BaseRest;
import org.yak.core.web.Result;
import org.yak.core.web.Result.Status;

import com.bkfm.framework.core.api.service.IBkfmTableMetaServer;
import com.bkfm.framework.core.entity.BkfmDataRecord;
import com.bkfm.framework.core.entity.BkfmFilter;
import com.bkfm.framework.core.entity.BkfmTableMetaData;
import com.bkfm.framework.core.entity.BkfmTreeModel;
import com.github.pagehelper.PageInfo;


/**
 * 
 *
 * @平台：统一开发平台
 * @文件名称：BkfmTableMetaRest.java
 * @文件描述：
 * @author xuqiang
 * @日期：2018年6月2日下午3:12:11
 *
 */
@RestController
@RequestMapping("/bkfm/metadata/v1/**")
public class BkfmTableMetaRest extends BaseRest<BkfmTableMetaData>{

	@Resource
	private IBkfmTableMetaServer tableMetaService;
	
	/**
	 * 查询业务模型表数据
	 * @param tableMetaData
	 * @return
	 */
	@RequestMapping("/querytablemetacolumn")
	public Result queryTableMetaColumn(BkfmFilter bkfmFilter){
		try {
			List<BkfmTableMetaData> columnlist = tableMetaService.queryTableMetaColumn(bkfmFilter);
			return new Result(Status.OK,columnlist);
		}catch(Exception e) {
			logger.error("获取业务模型数据异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 查询业务数据记录
	 * @param tableMetaData
	 * @param pageBounds
	 * @return
	 */
	@RequestMapping("/querytablemetacolumndatalist")
	public Result queryTableMetaColumnDataList(BkfmFilter bkfmFilter,PageParam pageBounds) {
		try {
			PageInfo<Map<String,Object>> list = tableMetaService.queryPageInfoMap(bkfmFilter, pageBounds);
			return new Result(Status.OK,"success",list);
		}catch(Exception e) {
			logger.error("查询业务数据异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 查询单挑业务表记录
	 * @param bkfmFilter
	 * @return
	 */
	@RequestMapping("/querytablemodelmetadata")
	public Result queryTableModelMetaData(BkfmFilter bkfmFilter) {
		try {
			Map<String,Object> metaData = tableMetaService.queryTableModelMetaData(bkfmFilter);
			return new Result(Status.OK,"success",metaData);
		}catch(Exception e) {
			logger.error("查询业务数据表单挑记录异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 保存或者修改数据记录
	 * @param dataRecord
	 * @return
	 */
	@RequestMapping("/savemodelmetadata")
	public Result saveModelMetaData(@RequestBody List<BkfmDataRecord> dataRecord) {
		try {
			tableMetaService.saveModelMetaData(dataRecord);
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("保存或者修改数据记录异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 新增
	 * @param dataRecord
	 * @return
	 */
	@RequestMapping("/addmodelmetadata")
	public Result addModelMetaData(@RequestBody BkfmDataRecord dataRecord) {
		try {
			tableMetaService.addModelMetaData(dataRecord);
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("保存或者修改数据记录异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 修改
	 * @param dataRecord
	 * @return
	 */
	@RequestMapping("/modifymodelmetadata")
	public Result modifyModelMetaData(@RequestBody BkfmDataRecord dataRecord) {
		try {
			tableMetaService.modifyModelMetaData(dataRecord);
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("保存或者修改数据记录异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param dataRecord
	 * @return
	 */
	@RequestMapping("/delmodelmetadata")
	public Result deleteModelMetaData(@RequestBody List<BkfmFilter> bkfmFilter) {
		try {
			tableMetaService.deleteModelMetaData(bkfmFilter);
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("保存或者修改数据记录异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 获取下啦选择框值
	 * @param bkfmFilter
	 * @return
	 */
	@RequestMapping("/getcombobox")
	public Result getComboBox(BkfmFilter bkfmFilter) {
		try{
			List<Map<String,Object>> list = tableMetaService.queryComboBoxMeta(bkfmFilter);
			return new Result(Status.OK,list);
		}catch(Exception e){
			logger.error("查询建模属性下拉列表值异常",e);
		}
		return null;
	}
	
	/**
	 * 树结构建模查询,返回的树接口数据key全部为小写
	 * @param param
	 * @return
	 */
	@RequestMapping("/gettree")
	public Result getTreeMeta(BkfmTreeModel param){
		try{
			String treeSql = TreeConfigCache.getParseTreeSql(param.getTreeConfigId(),request);
			if(!StringUtils.isEmpty(param.getFilter())){
				treeSql ="SELECT * FROM ("+treeSql+")TS WHERE " +param.getFilter();
			}
			param.setTreeSql(treeSql);
			List<Map<String,Object>> list =  tableMetaService.queryTreeMeta(param);
			return new Result(Status.OK,list);
		}catch(Exception e){
			logger.error("树窗体建模查询异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}

}
